package dao

import (
	"fanqiechaodan-blog/models"
	"go.uber.org/zap"
)

func GetUserNameById(uid int) (string, error) {
	sql := "SELECT user_name from blog_user WHERE uid = ?"
	row := DB.QueryRow(sql, uid)
	if row.Err() != nil {
		zap.S().Errorf("执行sql失败;sql:%s,uid:%s,err:%s", sql, uid, row.Err().Error())
		return "", row.Err()
	}
	var res string
	err := row.Scan(&res)
	if err != nil {
		zap.S().Errorf("Scan值失败;sql:%s,uid:%s,err:%s", sql, uid, err.Error())
		return "", row.Err()
	}
	return res, nil
}

func GetUser(username, passwd string) *models.User {
	var res models.User
	err := DB.SelectOne(&res, "SELECT * FROM blog_user WHERE user_name = ? AND passwd = ?", username, passwd)
	if err != nil {
		zap.S().Errorf("通过userName和passWd查询用户失败,userName:%s,passWd:%s,err:%s;", username, passwd, err.Error())
		return nil
	}
	return &res
}
